package com.songtech.universalservice.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.cursor.Cursor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.songtech.base.common.BaseConstant;
import com.songtech.base.common.JsonResult;
import com.songtech.base.universal.service.BaseService;
import com.songtech.universalservice.model.UniversalTest;

/**
 * 通用的service使用方式
 * <br>在controller中通过传入自定义的mybatis配置文件的方法和相应的map可以实现无特殊service处理的增删改查
 * <br>service的注入格式：@Resource(name = "com.songtech.universalservice.model.UniversalTest_service")，
 * <br>com.songtech.universalservice.model.UniversalTest是要注入service处理的entity对象，“_service”用于区分与基础dao注入的bean名字
 * <br>map对象：与自定义的mybatis中的#{}对应
 * <br>传入的方法名：mybatis的命名空间 + “.” + 方法名(namespace + "." + methodName)
 * <br>注意：UniversalService只能在controller中使用
 */
@Controller
@RequestMapping("/universaltest")
public class UniversalTestController {
	@Resource(name = "com.songtech.universalservice.model.UniversalTest_service")
	private BaseService<UniversalTest> baseService;
	
	/**
	 * 分页查询数据
	 * @param pageNum 页数
	 * @param pageSize 每页数量
	 * @return
	 */
	@RequestMapping("/selectAll/{pageNum}/{pageSize}")
	@ResponseBody
	public JsonResult selectAll(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
		Page<?> page = null;
		// 当pageNum pageSize小于或等于0时查询所有的数据
		if(pageNum > 0 && pageSize > 0){
			page = new Page<>(pageNum, pageSize);
		}
		Object info = null;
		try {
			// namespace + "." + methodName
			info = baseService.selectList("com.songtech.universal.dao.UniversalDao.getAll", page);
//			info = universalService.selectListUseRedis("com.songtech.universal.dao.UniversalDao.getAll", new Page<>());
//			info = universalService.selectListUseRedis("com.songtech.universal.dao.UniversalDao.getAll", page);
//			info = universalService.selectListUseRedis("com.songtech.universal.dao.UniversalDao.getAll", new HashMap<>());
//			info = universalService.selectListUseRedis("com.songtech.universal.dao.UniversalDao.getAll", null, null);
		} catch (SQLException e) {
			e.printStackTrace();
			return new JsonResult(info, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
		}
		
		return new JsonResult(info, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 分页查询map数据
	 * @param pageNum 页数
	 * @param pageSize 每页数量
	 * @return
	 */
	@RequestMapping("/selectMap/{pageNum}/{pageSize}")
	@ResponseBody
	public JsonResult selectMap(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize){
		Page<?> page = null;
		// 当pageNum pageSize小于或等于0时查询所有的数据
		if(pageNum > 0 && pageSize > 0){
			page = new Page<>(pageNum, pageSize);
		}

		PageInfo<?> info = null;
		// 查询返回map的2种方式
		try {
			info = baseService.selectMap("com.songtech.universal.dao.UniversalDao.getAll", "name", page);// 查询返回resultMap
//			info = universalService.selectMap("com.songtech.universal.dao.UniversalDao.getAllMap", "name_test", page);// 查询返回resultType
		} catch (SQLException e) {
			e.printStackTrace();
			return new JsonResult(info, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
		}
		
		return new JsonResult(info, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 查询一条数据数据
	 * @param id
	 * @return
	 */
	@RequestMapping("/selectOne01/{id}")
	@ResponseBody
	public JsonResult selectOne01(@PathVariable("id") String id){
		Map<String, String> map = new HashMap<>();
		map.put("id", id);
		UniversalTest ut = null;
		try {
//			ut = universalService.selectOne("com.songtech.universal.dao.UniversalDao.getOne", map);
			ut = (UniversalTest) baseService.selectOneUseRedis("com.songtech.universal.dao.UniversalDao.getOne", map);
		} catch (SQLException e) {
			e.printStackTrace();
			return new JsonResult(ut, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
		}
		
		return new JsonResult(ut, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 查询一条数据数据
	 * @param id
	 * @return
	 */
	@RequestMapping("/selectOne02/{id}")
	@ResponseBody
	public JsonResult selectOne02(@PathVariable("id") String id){
		Map<String, String> map = new HashMap<>();
		map.put("id", id);
		List<UniversalTest> list = null;
		try {
			list = baseService.select(new UniversalTest(id));
		} catch (SQLException e) {
			e.printStackTrace();
			return new JsonResult(list, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
		}
		
		return new JsonResult(list, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 查询一条数据数据
	 * <br>查询出list后自己处理
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/selectOne/{id}")
	@ResponseBody
	public JsonResult selectOne(@PathVariable("id") String id){
		Map<String, String> map = new HashMap<>();
		map.put("id", id);
		List<UniversalTest> list = null;
		try {
			list = (List<UniversalTest>) baseService.selectList("com.songtech.universal.dao.UniversalDao.getOne", map);
		} catch (SQLException e) {
			e.printStackTrace();
			return new JsonResult(list, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
		}
		
		return new JsonResult(list.size() > 0 ? (UniversalTest)list.get(0) : null, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 查询一条数据，使用基础dao查询方式
	 * @param id
	 * @return
	 */
	@RequestMapping("/getOne/{id}")
	@ResponseBody
	public JsonResult getOne(@PathVariable("id") String id){
		UniversalTest ut = null;
		try {
			ut = baseService.selectById(id);
		} catch (SQLException e) {
			e.printStackTrace();
			return new JsonResult(ut, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
		}
		
		return new JsonResult(ut, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 使用游标查询，对大量数据进行分批处理
	 * <br>查询存在异常，暂不使用
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/selectCursor")
	@ResponseBody
	public JsonResult selectCursor(){
		Integer num = 0;
		Cursor<UniversalTest> cursor;
		try {
			cursor = (Cursor<UniversalTest>) baseService.selectCursor("com.songtech.universal.dao.UniversalDao.getCursor");
		} catch (SQLException e) {
			e.printStackTrace();
			return new JsonResult(num, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
		}
		Iterator<UniversalTest> iter = cursor.iterator();
		List<UniversalTest> list = new ArrayList<>();
		int size = 10;// 每次处理的数据量
		// iter.hasNext()：Iterator中有数据
		while (iter.hasNext()) {
			// 循环遍历
		    for(int i = 0; i < size && iter.hasNext(); i ++) {
		    	list.add(iter.next());
		    }
		    // 对list中的数据进行处理
		    this.printList(list);
		    System.out.println();
		    num += list.size();
		    // 清空list
		    list.clear();
		}
		
		return new JsonResult(num, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 打印list中的数据
	 * @param list
	 */
	private void printList(List<UniversalTest> list){
		for(UniversalTest ut : list){
			System.out.println(ut.toString());
		}
	}
}
